package org.jsbd.boss.action;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.domian.UmAppInfo;
import org.jsbd.boss.service.IUmAppInfoService;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.utils.StringUtils;

@Controller
@RequestMapping("/umAppInfo")
public class UmAppInfoController {

	public static final Logger logger = LoggerFactory.getLogger(UmAppInfoController.class);

	@Autowired
	private IUmAppInfoService umAppInfoService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "umAppInfo/list";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(UmAppInfo umAppInfo) {
		if (StringUtils.isBlank(umAppInfo.getApkName())) {
			return ResponseMapUtils.responseFail("名称不能为空");
		}
		umAppInfoService.save(umAppInfo);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(UmAppInfo umAppInfo) {
		if (umAppInfo.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		UmAppInfo obj = umAppInfoService.getById(umAppInfo.getId());
		obj.setPackageName(umAppInfo.getPackageName());
		obj.setAppVersionCode(umAppInfo.getAppVersionCode());
		obj.setAppVersionName(umAppInfo.getAppVersionName());
		obj.setAppKey(umAppInfo.getAppKey());
		obj.setAppSecret(umAppInfo.getAppSecret());
		obj.setApkSignatureMD5(umAppInfo.getApkSignatureMD5());
		obj.setApkName(umAppInfo.getApkName());
		obj.setPageName(umAppInfo.getPageName());
		umAppInfoService.update(obj);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "getListPage")
	@ResponseBody
	public Map<String, Object> getListPage(DataGridModel page, UmAppInfo umAppInfo) throws Exception {
		return umAppInfoService.getListPage(page, umAppInfo);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		try {
			List<Integer> list = Arrays.asList(ids);
			umAppInfoService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}
}